package jp.co.sharp.android.parents.kidsguard.activity.edit.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.dto.ScheduleDetailDto;
import jp.co.sharp.android.parents.kidsguard.dto.ScheduleDto;

public class ScheduleListItemAdapter extends ArrayAdapter<ScheduleDetailDto>{

    private int resourceId;
    private OnClickItemListenter onClickItemListenter;


    public ScheduleListItemAdapter(@NonNull Context context, int resource, @NonNull List<ScheduleDetailDto> objects, OnClickItemListenter onClickItemListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.onClickItemListenter = onClickItemListenter;
    }




    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ScheduleDetailDto scheduleDetailDto = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.textViewTitle = view.findViewById(R.id.textViewTitle);
            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.textViewTitle.setText(scheduleDetailDto.getTitle());
        viewHolder.textViewTitle.setOnClickListener(new ChooseItemListenter());
        ScheduleDetailDto scheduleDetailDtoResult = new ScheduleDetailDto();
        scheduleDetailDtoResult.setId(scheduleDetailDto.getId());
        scheduleDetailDtoResult.setDate(scheduleDetailDto.getDate());
        viewHolder.textViewTitle.setTag(scheduleDetailDtoResult);
        return view;
    }


    class ViewHolder{
        TextView textViewTitle;
    }

    /**
     *
     */
    public interface OnClickItemListenter {
        void chooseItemClick(ScheduleDetailDto Object, View view);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void OnDelBluetoothListenter(OnClickItemListenter callBack) {
        onClickItemListenter = callBack;
    }

    class ChooseItemListenter implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            onClickItemListenter.chooseItemClick((ScheduleDetailDto)view.getTag(), view);
        }
    }
}
